﻿#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

实现栈（10分钟）
--------------------------------------------------------------------------------------------------------------------------------------
typedef int StackDataType;
typedef struct stack
{
    StackDataType* data;
    int size;
    int capacity;
}Stack;

void stackInit(Stack* pst)
{
    assert(pst);
    StackDataType* temp = (StackDataType*)malloc(sizeof(StackDataType) * 4);
    if (temp == NULL)
    {
        perror("malloc fail!");
        exit(-1);
    }
    pst->data = temp;
    pst->size = 0;
    pst->capacity = 4;
}

void stackDestroy(Stack* pst)
{
    assert(pst);
    free(pst->data);
    pst->data = NULL;
    pst->size = 0;
    pst->capacity = 0;
}

void checkcapacity(Stack* pst)
{
    assert(pst);
    if (pst->capacity == pst->size)
    {
        StackDataType* temp = (StackDataType*)realloc(pst->data, sizeof(StackDataType) * pst->capacity * 2);
        if (temp == NULL)
        {
            perror("realloc fail!");
            exit(-1);
        }
        pst->data = temp;
        pst->capacity = pst->capacity * 2;
    }
}

int stackisempty(Stack* pst)
{
    assert(pst);
    if (pst->size == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void stackpush(Stack* pst, StackDataType data)
{
    assert(pst);
    checkcapacity(pst);
    pst->data[pst->size] = data;
    pst->size++;
}

void stackpop(Stack* pst)
{
    assert(pst);
    assert(pst->size > 0);
    pst->size--;
}

StackDataType stacktop(Stack* pst)
{
    assert(pst);
    assert(pst->size > 0);
    return pst->data[pst->size - 1];
}

int stackSize(Stack* pst)
{
    assert(pst);
    return pst->size;
}

--------------------------------------------------------------------------------------------------------------------------------------

965.单值二叉树
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
    bool isUnivalTree(struct TreeNode* root) {
    if (root == NULL)
    {
        return true;
    }
    if (root->left)
    {
        if (root->val != root->left->val || !isUnivalTree(root->left))
        {
            return false;
        }
    }
    if (root->right)
    {
        if (root->val != root->right->val || !isUnivalTree(root->right))
        {
            return false;
        }
    }
    return true;
}